package net.udp;

import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class UDPDiscardServerWithChannels
{
	public final static int DEFAULT_PORT = 9;
	public final static int MAX_PACKET_SIZE = 65507;
	public static void main(String[] args)
	{
		int port = DEFAULT_PORT;
		try
		{
			DatagramChannel channel = DatagramChannel.open();
			DatagramSocket socket = channel.socket();
			SocketAddress address = new InetSocketAddress(port);
			socket.bind(address);
			ByteBuffer buffer = ByteBuffer.allocateDirect(MAX_PACKET_SIZE);
			while(true)
			{
				SocketAddress client = channel.receive(buffer);
				buffer.flip();
				System.out.println(client);
				while(buffer.hasRemaining())
				{
					System.out.write(buffer.get());
				}
				buffer.clear();
			}
			
		}
		catch (Exception e)
		{
		}
	}
}
